<style>

</style>

<template>
  <p>
    <a-space>
        <a-button type="primary" @click="onAdd">新增</a-button>
        <a-button type="primary" @click="handleQuery()">刷新</a-button>
    </a-space>
    </p>
    <a-table 
        :dataSource="stations" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
        <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'operation'">
                <a-space>
                    <a-popconfirm
                        title="删除后不可恢复，确认删除?"
                        @confirm="onDelete(record)"
                        ok-text="确认"
                        cancel-text="取消"
                    >
                        <a style="color: red">删除</a>
                    </a-popconfirm>
                
                    <a @click="onEdit(record)">编辑</a>
                </a-space>
            </template>
        </template>
    </a-table>
    <a-modal v-model:visible="visible" title="车站" @ok="handleOk" ok-text="确认" cancel-text="取消">
        <a-form :model="station" :label-col="{span: 4}" :wrapper-col="{span: 14}">
            <a-form-item label="站名">
                <a-input v-model:value="station.name"/>
            </a-form-item>
            <a-form-item label="站名拼音">
                <a-input v-model:value="station.namePinyin" disabled/>
            </a-form-item>
            <a-form-item label="站名拼音字母" >
              <a-input v-model:value="station.namePy" disabled/>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
import { defineComponent,ref,onMounted,watch} from 'vue';
import { notification } from 'ant-design-vue';
import {pinyin} from 'pinyin-pro'
import axios from 'axios';

export default defineComponent({
  name: "station-view",
  setup() {
    // 展示模态窗的标签的定义
    const visible = ref(false);
    const loading = ref(false);

    // 定义车站对象
    let station = ref({
        id: undefined,
        name: undefined,
        namePinyin: undefined,
        namePy: undefined,
        createTime: undefined,
        updateTime: undefined
    });

    let stationTypeEnums = ref([])

    const stations = ref([]);
    
    const columns = [
        {
            title: '站名',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: '站名拼音',
            dataIndex: 'namePinyin',
            key: 'namePinyin',
        },
        {
            title: '站名拼音首字母',
            dataIndex: 'namePy',
            key: 'namePy',
        },
        {
            title: '操作',
            dataIndex: 'operation',
            key: 'operation',
        },
    ];

   

    let pagination =ref({
        total: 0,
        current: 1,
        pageSize: 10,
    });

    // 点击新增展示模态窗的函数方法
    const onAdd = () => {
        console.log("add")
        visible.value = true;
        station.value = {}
    };

    // 点击编辑展示模态窗的函数方法
    const onEdit = (record) => {
        console.log("edit")
        station.value = window.Tool.copy(record)
        visible.value = true;
    };

     // 点击删除按钮的函数方法
    const onDelete = (record) => {
        axios.delete("/business/admin/station/delete/"+record.id)
        .then( response => {
            console.log(response.data.data)
            if(response.data.data){
                notification.success({ description: '删除成功' });
                // 刷新页面
                handleQuery()
            }
        })
        .catch( error => {
            notification.error({ description:  error.response.data.data});
        })
    }

    // 点击模态窗的ok按钮执行的函数方法
    const handleOk = e => {
        axios.post("/business/admin/station/save", station.value)
            .then( response => {
                let respData = response.data
                console.log(respData)
                if(respData.data){
                    notification.success({ description: '保存成功！' });
                    // 设置为false，关闭模态窗
                    visible.value = false;
                    // 调用列表查询的接口，刷新数据
                    handleQuery()
                }else{
                    notification.error({ description: respData.errorMessage });
                }
            })
            .catch( error => {
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 车站信息列表查询的方法
    const handleQuery = e => {
        // 刷新时候不带参数的判断
        if(!e){
            e = {
                page: 1,
                limit: pagination.value.pageSize
            }
        }
        // 开启loading效果
        loading.value = true
        axios.get("/business/admin/station/query",
            {
                params:{
                    page: e.page,
                    limit: e.limit
                }
            })
            .then( response => {
                // 关闭loading效果
                loading.value = false
                let respData = response.data
                let passengerDataList = respData.data.dataList
                let total = respData.data.total
                let currentPage = respData.data.currentPage
                let pageSize = respData.data.pageSize
                console.log(respData.data.dataList)
                stations.value = passengerDataList
                // 分页控件的设置
                pagination.value.current = currentPage
                pagination.value.pageSize = pageSize
                pagination.value.total = total
            })
            .catch( error => {
                // 关闭loading效果
                loading.value = false
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 页码的点击事件
    const handleTableChange = (pagination) => {
        console.log("pagination:",pagination.value)
        // 调用车站列表查询方法
        handleQuery({
            page: pagination.current,
            limit: pagination.pageSize
        })
    };
    // 钩子函数，页面初始化的时候执行
    onMounted(() => {
        handleQuery({
            page: pagination.value.current,
            limit: pagination.value.pageSize
        })
    });

    // watch计算函数监听name属性的变化
    watch(() => station.value.name, () => {
        if(window.Tool.isNotEmpty(station.value.name)){
            station.value.namePinyin = pinyin(station.value.name,{toneType:'none'}).replaceAll(" ","")
            station.value.namePy = pinyin(station.value.name,{pattern:'first',toneType:'none'}).replaceAll(" ","").toLocaleUpperCase()
        }else{
            station.value.namePinyin = ""
            station.value.namePy = ""
        }
    }, {immediate:true})

    return {
        visible,
        loading,
        station,
        stations,
        columns,
        pagination,
        stationTypeEnums,
        onAdd,
        onEdit,
        onDelete,
        handleOk,
        handleQuery,
        handleTableChange
    };
  },
});
</script>